//
//  WAOrderSheetView.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/16.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAOrderSheetView.h"
#import "WAOrderEditView.h"

@interface WAOrderSheetView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIButton *bugButton;
@property (nonatomic, strong) UIButton *publishBtn;
@property (nonatomic, strong) WAOrderEditView *orderEditView;
@end

@implementation WAOrderSheetView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentView.frame = CGRectMake(0, 0, WNScreenWidth, 180 + self.safe_bottom);
        [self initUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)initUI{
    self.publishBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 0, WNScreenWidth - 30, 40)];
    [self.publishBtn setTitle:@"发布" forState:0];
    [self.publishBtn setBottom:self.contentView.height - 15 - self.safe_bottom];
    self.publishBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.publishBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.publishBtn addTarget:self action:@selector(publishAction) forControlEvents:UIControlEventTouchDown];
    [self.contentView addSubview:self.publishBtn];
    self.publishBtn.backgroundColor = [UIColor colorWithHexString:@"777484"];
    self.publishBtn.layer.masksToBounds = YES;
    self.publishBtn.layer.cornerRadius = 4;
    
    UIView *topContainView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, WNScreenWidth - 30, self.publishBtn.top - 15)];
    [self.contentView addSubview:topContainView];
    topContainView.backgroundColor =[UIColor colorWithHexString:@"777484"];
    topContainView.layer.masksToBounds = YES;
    topContainView.layer.cornerRadius = 4;
    
    self.orderEditView = [[WAOrderEditView alloc] initWithFrame:topContainView.bounds];
    [topContainView addSubview:self.orderEditView];
    self.orderEditView.jiageTF.delegate = self;
}

- (void)show{
    [super show];
    if (self.type == 0) {
        self.orderEditView.jiageTF.placeholder = [NSString stringWithFormat:@"%.1f~%.1f",self.loginManager.buy_minValue,self.loginManager.buy_maxValue];
    }else{
        self.orderEditView.jiageTF.placeholder = [NSString stringWithFormat:@"%.1f~%.1f",self.loginManager.sell_minValue,self.loginManager.sell_maxValue];
    }
}

- (void)publishAction{
    
    NSInteger number = self.orderEditView.shuliangTF.text.integerValue;
    NSString *price = self.orderEditView.jiageTF.text;
    if (number <= 0) {
        [WNTool show:@"数量填写错误！"];
        return;
    }
    if (price.floatValue <= 0) {
        [WNTool show:@"价格填写错误！"];
        return;
    }
    if (number < 10) {
        [WNTool show:@"数量至少要10个"];
        return;
    }
    
    if (self.orderClickBlock) {
        self.orderClickBlock(self.type,number,price);
    }
    UITextField *tf = [self.window performSelector:@selector(firstResponder)];
    if (tf) {
        [tf resignFirstResponder];
    }
    [self dismiss];
}


- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyBoardframe = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat spaceH = 10;
    CGFloat margin = keyBoardframe.size.height + spaceH;
    [UIView animateWithDuration:0.2 animations:^{
        self.contentView.transform = CGAffineTransformMakeTranslation(0, - margin);
    } completion:^(BOOL finished) {
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [UIView animateWithDuration:0.2 animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}

//参数一：range，要被替换的字符串的range，如果是新输入的，就没有字符串被替换，range.length = 0
//参数二：替换的字符串，即键盘即将输入或者即将粘贴到textField的string
//返回值为BOOL类型，YES表示允许替换，NO表示不允许
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
    //新输入的
    if (string.length == 0) {
        return YES;
    }

   //第一个参数，被替换字符串的range
   //第二个参数，即将键入或者粘贴的string
   //返回的是改变过后的新str，即textfield的新的文本内容
    NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    //正则表达式（只支持两位小数）
    NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
   //判断新的文本内容是否符合要求
    return [self isValid:checkStr withRegex:regex];

}

//检测改变过的文本是否匹配正则表达式，如果匹配表示可以键入，否则不能键入
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}
@end
